const { Get, Controller } = require("../decorator/router")
const {
    IPHOMES_MODEL,
    INTELLIGENT_MODEL,
    GAMEDEVICES_MODEL,
    ELECTRONICS_MODEL,
    POWEACCESSORES_MODEL,
    MOBILESTORAGES_MODEL,
    CIRCUMS_MODEL
} = require("../dataDB/homeData")
const {
    JudgeAnObject,
    dataErrorReturn
} = require("../util/util")

@Controller('/navigation')
class iphomes {
    @Get('/:id')
    async RequestAllMobileData(ctx, next) {
        let keys = Object.keys(ctx.query).filter(elem => elem !== 'skip' && elem !== 'limit' && elem !== 'price')
        console.log(ctx.query)
        let parameter = {}
        keys.forEach((item, index) => {
            if (item === 'fuselageCapacity') {
                parameter[item] = { $in: [ctx.query[item] + 'GB'] }
            } else {
                parameter[item] = ctx.query[item]
            }
        })
        let sort = null
        let skip = parseInt(ctx.query.skip) || 0
        let limit = parseInt(ctx.query.limit) || 8
        if (ctx.query.price) {
            sort = {
                price: ctx.query.price
            }
        } else {
            sort = {}
        }
        let dataUrl = null
        switch (ctx.params.id) {
            case "iphone":
                dataUrl = await IPHOMES(IPHOMES_MODEL, parameter, skip, limit, sort)
                break
            case "intelligentdevices":
                dataUrl = await IPHOMES(INTELLIGENT_MODEL, parameter, skip, limit, sort)
                break
            case "gamedevices":
                dataUrl = await IPHOMES(GAMEDEVICES_MODEL, parameter, skip, limit, sort)
                break
            case "electronics":
                dataUrl = await IPHOMES(ELECTRONICS_MODEL, parameter, skip, limit, sort)
                break
            case "poweraccessories":
                dataUrl = await IPHOMES(POWEACCESSORES_MODEL, parameter, skip, limit, sort)
                break
            case "mobilestorages":
                dataUrl = await IPHOMES(MOBILESTORAGES_MODEL, parameter, skip, limit, sort)
                break
            case "circums":
                dataUrl = await IPHOMES(CIRCUMS_MODEL, parameter, skip, limit, sort)
                break
        }
        
        dataErrorReturn({ dataUrl, ctx })
    }
}

async function IPHOMES(MODEL, parameter, skip, limit, sort) {
    let dataUrl = await MODEL.find(parameter, {
        checked: false,
        fuselageCapacity: false,
        sort: false,
        brand: false,
        screenSize: false,
        __v: false,
        _id: false
    }).skip(skip).limit(limit).sort(sort)
    return Promise.resolve(dataUrl)
}



// let dataUrl = await IPHOMES_MODEL.find(parameter, {
        //     checked: false,
        //     fuselageCapacity: false,
        //     sort: false,
        //     brand: false,
        //     screenSize: false,
        //     __v: false,
        //     _id: false
        // }).skip(skip).limit(limit).sort(sort)